package com.zhongxi.guoyuan.adapter.shopCart;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;

import java.util.List;

/**
 * Created by Administrator on 2017/2/4.
 */

public class AttrValueCartAdapter extends BaseAdapter {

    private Context mContext;
    private List<String> list;
    private int selection = -1;


    public AttrValueCartAdapter(Context mContext, List<String> list, int pos) {
        this.mContext = mContext;
        this.list = list;
        this.selection = pos;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try{
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_attr_gv, parent, false);
                holder.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            if(null!=list.get(position)&&!"".equals(list.get(position))){
                holder.tvContent.setText(list.get(position));
            }else{
                holder.tvContent.setText("");
            }
            if(position == selection){
                holder.tvContent.setSelected(true);
            }else{
                holder.tvContent.setSelected(false);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    public void setSelect(int position) {
        this.selection = position;
    }

    class ViewHolder{
        private TextView tvContent;
    }
}
